import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:xianyu_school/utils/url.dart';
import 'package:xianyu_school/xianyuApp/entity/goods/goods.dart';
import 'package:xianyu_school/xianyuApp/home/List/homeGoodItem.dart';

import 'Widgets/BallPulseIndicator.dart';

class myBoughtPage extends StatelessWidget{

  List<goods> gs = [];
  String url = urls().url;

  @override
  Widget build(BuildContext context) {

    Future getgoods() async {
      try{
        SharedPreferences pref = await SharedPreferences.getInstance();
        String username = pref.getString("username");
        Response re = await Dio().get(url+"/users/getBought",queryParameters: {"username":username});
        return re;
      }
      catch(e){
        return ;
      }
    }

    return Scaffold(
      appBar: CupertinoNavigationBar(
        backgroundColor: Colors.yellow,
        middle: Text('我购买的'),
      ),
      body:FutureBuilder(
        future: getgoods(),
        builder: (context,snapshot){
          if(snapshot.hasData){
            if(snapshot.data.toString()=="No more data"){
              Fluttertoast.showToast(
                  msg: "暂无数据",
                  toastLength: Toast.LENGTH_SHORT,
                  gravity: ToastGravity.BOTTOM,
                  timeInSecForIosWeb: 1,
                  backgroundColor: Colors.black45,
                  textColor: Colors.white,
                  fontSize: 16.0
              );
              return Text(" ");
            }
            var responseJson = json.decode(snapshot.data.toString());
            responseJson.forEach((data) {
              gs.add(goods.fromJson(data));
            });
            int sum = 0;
            for(goods g in gs){
              sum = sum + g.price;
            }
            return ListView(
              children: [
                Container(
                  height: 480,
                  child: CustomScrollView(
                    slivers: [
                      // 商品列表
                      SliverPadding(
                        padding: EdgeInsets.fromLTRB(10, 5, 10, 10),
                        sliver: SliverStaggeredGrid.countBuilder(
                          crossAxisCount: 1,
                          crossAxisSpacing: 10,
                          mainAxisSpacing: 10,
                          itemCount: gs.length,
                          itemBuilder: (BuildContext context, int index) {
                            return FHHomeGoodItem(model: gs[index],);
                          },
                          staggeredTileBuilder: (int index) {
                            return StaggeredTile.fit(2);
                          },
                        ),
                      ),
                    ],
                  ),
                ),
                Container(
                  child: Row(
                    children: [
                      Expanded(child: Text(" ")),
                      Text("总价："),
                      Text(
                        "￥"+sum.toString(),
                        style: TextStyle(
                          color: Colors.red,
                          fontSize: 20
                        ),
                      ),
                      Text("     "),
                    ],
                  ),
                )
              ],
            );
          }
          else return Center(
            child: BallPulseIndicator(
                color: Colors.white
            ),
          );
        },
      ),
    );
  }
}